package com.xjprojectc.uitl;

import java.util.List;


/**
* 分页Page类
* 
* @param <T>
*/
public class Page<T> {
	public final int DEFAULT_PAGESIZE = AppContext.PAGE_SIZE; // 每页记录数
	private List<T> result = null;// 页面数据集合
	private int totalRows;// 总记录数
	private int pageSize = DEFAULT_PAGESIZE;// 每页显示行数
	private int currentPage;// 当前页数
	private int totalPages;// 总页数
	private int startRow;// 查询开始的记录数

	public Page() {

	}

	public Page(int totalRows) {
		if (totalRows > 0) {
			// 初始化总记录数
			this.totalRows = totalRows;
			// 初始化总页数
			this.totalPages = totalRows / pageSize;
			if (totalRows % pageSize > 0)
				totalPages++;

		} else {
			this.totalRows = 0;
		}

		// 初始化当前页数
		this.currentPage = 1;
		// 初始化查询开始的记录数
		this.startRow = 0;
	}

	// 页内的数据列表.

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	// 获得上一页
	public void previous() {
		if (currentPage == 1)
			return;
		currentPage--;
		startRow = (currentPage - 1) * pageSize;
	}

	// 获得下一页
	public void next() {
		if (currentPage == totalPages) {
			return;
		}
		currentPage++;
		startRow = (currentPage + 1) * pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	// 设置当前页
	public void setCurrentPage(int currentPage) {

		if (currentPage >= totalPages) {
			if (totalPages == 0)
				this.currentPage = 1;
			else
				this.currentPage = totalPages;
		}
		if (1 < currentPage && currentPage < totalPages) {
			this.currentPage = currentPage;
		}
		if (currentPage < 1) {
			this.currentPage = 1;
		}

	}

	public int getStartRow() {
		startRow = (currentPage - 1) * pageSize;
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public int getPageSize() {
		return pageSize;
	}

	public boolean isStart() {
		return currentPage == 1;
	}

	public boolean isEnd() {
		return currentPage == totalPages || totalPages == 0;
	}

}

